﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Common;
using Lib.Utility;
using Module.BusinessLogic;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Components.Audits;
using Module.Core.LoginSystem.Components.Modules;

namespace Web.Cms.ClubName
{
    public partial class ClubNameManager : AdminUserControl
    {
        #region Khai báo biến
        private int totalRecords;
        private int cPage = 1;
        private DateTime dateTo;
        private DateTime dateFrom;
        #endregion

        #region lấy giá trị khi request
        void GetRequest()
        {
            if (Request.QueryString["keyword"] != null || Request.QueryString["keyword"] != "")
                txtKeysearch.Text = Request.QueryString["keyword"];

            if (Request.QueryString["dateFrom"] != null && Request.QueryString["dateFrom"] != "")
            {
                dateFrom = DateTime.Parse(Request.QueryString["dateFrom"], CultureInfo.CreateSpecificCulture("vi-VN"));
                txtDatefrom.Text = dateFrom.ToString("dd/MM/yyyy");
            }
            else
            {
                dateFrom = DateTime.Now.AddMonths(-2);
                txtDatefrom.Text = dateFrom.ToString("dd/MM/yyyy");
            }

            if (Request.QueryString["dateTo"] != null && Request.QueryString["dateTo"] != "")
            {
                dateTo = DateTime.Parse(Request.QueryString["dateTo"], CultureInfo.CreateSpecificCulture("vi-VN"));
                txtDateTo.Text = dateTo.ToString("dd/MM/yyyy");
            }
            else
            {
                dateTo = DateTime.Now;
                txtDateTo.Text = dateTo.ToString("dd/MM/yyyy");
            }
            if (Request.QueryString["zoneId"] != null || Request.QueryString["zoneId"] != "")
            {
                dropZoneId.SelectedValue = Request.QueryString["zoneId"];
            }
        }
        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //Fill dữ liệu category
            GetRequest();
        }

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                base.OnPreRender(e);
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            if (Request.QueryString.Get("cpage") != null)
                cPage = ConvertUtility.ToInt32(Request.QueryString.Get("cPage"));
            int from = (cPage - 1) * ObjConstants.pageSize20 + 1;
            if (from == 0) from = 1;
            int to = (cPage) * ObjConstants.pageSize20;

            totalRecords = CMSClubName.GetCount(txtKeysearch.Text, status, dateFrom, dateTo.AddDays(+1), from, to);

            Paging1.totalRecords = totalRecords;
            Paging1.numPerPage = ObjConstants.pageSize20;
            Paging1.queryParam = MiscUtility.GetCPPage("ListClubNamePublic",
                                    new string[] { "keyword", "Status", "from", "to", "dateFrom", "dateTo", "cPage" },
                                    new string[] { txtKeysearch.Text,status.ToString(), from.ToString(), to.ToString(), txtDatefrom.Text, txtDateTo.Text, "" });

            DataTable dt;
            string order = null;
            dt = CMSClubName.Search(txtKeysearch.Text, status, dateFrom, dateTo.AddDays(+1), from, to, order);
            if (dt != null && dt.Rows.Count > 0)
            {
                grdData.DataSource = dt;
                grdData.RowDataBound += new GridViewRowEventHandler(grdData_RowDataBound);
                grdData.DataBind();
            }
            else
            {
                grdData.Visible = false;
                if (Request.QueryString["keyword"] != null && Request.QueryString["keyword"] != "")
                    lblMessage.Text = "Không tìm thấy dữ liệu";
                else
                    lblMessage.Text = "Dữ liệu đang được cập nhật";
            }

        }

        protected void grdData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int clubId = ConvertUtility.ToInt32(e.CommandArgument);
                switch (e.CommandName)
                {

                    case "CommandStatus":

                        if (status == 1)// câu lạc bộ đang hiện thị ->gỡ xuống update status=2
                        {
                            CMSClubName.UpdateStatus(clubId, 2, 1);
                        }

                        break;

                    case "Commanddelete":

                        CMSClubName.Delete(clubId);// xóa câu lạc bộ

                        // Insert Cores_Audits
                        ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                        AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Delete, CurrentCommand.CtrlKey, CurrentUser.UserID, clubId
                                                    , "Xóa câu lạc bộ Id:" + clubId, 1, DateTime.Now);
                        //end                       

                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton btnStatus = (ImageButton)e.Row.FindControl("btnStatus");
                e.Row.Cells[0].Text = (ObjConstants.pageSize20 * (cPage - 1) + e.Row.DataItemIndex + 1).ToString();
                if (status == 1)
                {
                    btnStatus.ImageUrl = "/App_Themes/Default/Images/Buttons/btn_pending.gif";
                    btnStatus.OnClientClick = "return confirm('Bạn có ngừng hiện thị câu lạc này xuống không ?')";
                }
                if (status == 2)
                {
                    btnStatus.ImageUrl = "/App_Themes/Default/Images/Buttons/btn_forward.gif";
                }

            }
        }
        #endregion

        #region tìm kiếm
        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Cms/Default.aspx?ctrlkey=ListClubNamePublic&keyword=" + txtKeysearch.Text.Trim() + "&zoneId=" + dropZoneId.SelectedValue + "&dateFrom=" + txtDatefrom.Text.Trim().ToString() + "&dateTo=" + txtDateTo.Text.Trim());
        }
        #endregion

        private int status;
        public int Status
        {
            set { status = value; }
            get { return status; }
        }
    }
}